﻿using System;
using System.IO;
using Google.Protobuf.Compiler;
using UnityEditor;
using UnityEngine;

namespace Wxy.Res.Editor
{
    public class CreateWindowEditor : EditorWindow
    {
        private static string _moduleName = "Module Name";
        private static CreateWindowEditor _windowEditor;
        [MenuItem("Wfp/创建UI窗口预制体和脚本", false, 4)]
        public static void ShowWindow()
        {
            _windowEditor = GetWindow<CreateWindowEditor>("Create UI Window");
        }

        private void OnGUI()
        {
            GUILayout.Label("Enter Window Name", EditorStyles.boldLabel);

            string rule = @"
快速创建UI窗口预制体和脚本,约定规则如下:
输入模块窗口名字，不能和项目中已有的Ui模块重名,点击Create按钮,会在以下目录创建窗口预制体和脚本:
预制体目录: Assets/ABRes/UI/{ModuleName}/Prefabs/{ModuleName}Window.prefab
脚本目录: Assets/HotUpdate/UI/{ModuleName}/{ModuleName}Window.cs
最后创建完成后，手动将生成的脚本挂载到生成的预制体上
";
            //绘制上面的规则，字体大小可设置
            GUILayout.Label(rule, EditorStyles.wordWrappedLabel);

            _moduleName = EditorGUILayout.TextField("Module Name", _moduleName);

            if (GUILayout.Button("Create"))
            {
                _moduleName = _moduleName.Trim();
                CreateWindowFiles(_moduleName);
            }
        }

        public static bool isCompiling = false;

        private void CreateWindowFiles(string name)
        {
            isCompiling = false;
            string prefabDirectory = $"Assets/ABRes/UI/{name}/Prefabs";
            string scriptDirectory = $"Assets/HotUpdate/UI/{name}";

            string prefabPath = $"{prefabDirectory}/{name}Window.prefab";
            string scriptPath = $"{scriptDirectory}/{name}Window.cs";


            if (!AssetDatabase.IsValidFolder(prefabDirectory))
            {
                Directory.CreateDirectory(prefabDirectory);
            }
            else if (File.Exists(prefabPath))
            {
                EditorUtility.DisplayDialog("Error", "Prefab already exists!", "OK");
                return;
            }

            if (!AssetDatabase.IsValidFolder(scriptDirectory))
            {
                Directory.CreateDirectory(scriptDirectory);
            }
            else if (File.Exists(scriptPath))
            {
                EditorUtility.DisplayDialog("Error", "Script already exists!", "OK");
                return;
            }

            // Create the script file
            string scriptTemplate = $@"using HotUpdate.GUI;
namespace HotUpdate.UI
{{
    public class {name}Window : UIWindowBase
    {{
      
    }}
}}
";
            File.WriteAllText(scriptPath, scriptTemplate);

            AssetDatabase.Refresh();
            isCompiling = true;
            //Debug.Log("Begin Compile");

            AfterCompile();
        }

        public static void AfterCompile()
        {
            isCompiling = false;
            GameObject newWindow = new GameObject($"{_moduleName}Window");
            var image = newWindow.AddComponent<UnityEngine.UI.Image>();
            image.rectTransform.sizeDelta = new Vector2(2388, 1668);
            image.rectTransform.anchorMin = new Vector2(0, 0);
            image.rectTransform.anchorMax = new Vector2(1, 1);
            image.rectTransform.pivot = new Vector2(0.5f, 0.5f);
            image.rectTransform.offsetMin = new Vector2(0, 0);
            image.rectTransform.offsetMax = new Vector2(0, 0);
            string prefabDirectory = $"Assets/ABRes/UI/{_moduleName}/Prefabs";
            string prefabPath = $"{prefabDirectory}/{_moduleName}Window.prefab";
            PrefabUtility.SaveAsPrefabAsset(newWindow, prefabPath);
            DestroyImmediate(newWindow);
            EditorUtility.DisplayDialog("Success", "UI模块预制体和脚本创建完成!", "OK");
            _windowEditor.Close();
            _windowEditor = null;
        }
    }
}